import requests
import json
from config.ip import *
from common.log import logger
from common.time_processing import time1
from common.randoms import ran
import time

headers = {'content-type': 'application/json;charset=UTF-8', 'token': token}
# print(headers)
headers1 = {'accept': 'application/json, text/plain, */*'}
response = requests.post('https://xx', headers1)
print(response.text)
'''
当前页面接口
1.新建运单
2.获取新建运单的id
3.根据运单id和调度单号创建调度单并且返回调度单id
4.根据调度单id获取调度单里面的运单号
5.调度单派单
6.调度单发车
7.调度单到达
8.回单上传
'''
rand = ran()


# print(rand.randStr(5))
# print(rand.randInt(5))
def check_waybill(times, freight):
    '''
    :param time: 提货时间
    :param freight: 运费
    :return: 检验运费不能为0和要求提货时间与录单时间间隔不能大于30天
    '''
    path = 'xx'
    url = http + ip + path
    headers = {'content-type': 'application/json;charset=UTF-8', 'token': token}
    data = {
        "waybillNo": "Y21051702015000001",
        "requiredDeliveryDate": times,
        "freight": freight,
        "serviceType": None,
        "receiptPayment": "",
        "showFreight": True
    }
    response = requests.post(url, data=json.dumps(data), headers=headers)
    return response.json()['content']


# print(check_waybill(1618156800000,0))

def new_waybill(waybillNo, projectNo):
    # logger.debug('正在调用新建运单接口')
    path = 'xx'
    url = http + ip + path
    headers = {'content-type': 'application/json;charset=UTF-8', 'token': token}
    # 运单号  不能重复     waybillNo
    # 客户订单号 使用随机数
    customerOrderNo = rand.randStr(10)
    # 项目编号 必须存在的项目的编号 projectNo
    # logger.debug(
    #     '参数-运单号waybillNo:{},客户订单号customerOrderNo:{},项目编号projectNo:{}'.format(waybillNo, customerOrderNo, projectNo))
    data = {
        "waybillNo": waybillNo,
        "projectNo": projectNo,
        "customer": "合伙",
        "customerNo": "C1620357482152185",
        "customerOrderNo": customerOrderNo,
        "goodsList": [{
            "goodsName": "货物名称",
            "goodsWeight": 1,
            "goodsVolume": 1,
            "goodsNums": 1,
            "goodsWorth": 1,
            "goodsCosts": 1,
            "goodsUnit": "1"
        }],
        "showFreight": True
    }
    response = requests.post(url, data=json.dumps(data), headers=headers)
    # ['content']['waybillNo']
    return response.json()


def get_waybill_id(waybillNoList):
    # logger.debug('正在调用获取运单id接口,可用于新建调度单和删除运单')
    path = 'xx'
    url = http + ip + path
    headers = {'content-type': 'application/json;charset=UTF-8', 'token': token,
               'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'}
    # 运单号可多选列表格式 ["Y210507020150000013"]
    # logger.debug('参数-运单号列表waybillNoList：{}'.format(waybillNoList))
    data = {
        "waybillNoList": [],
        "customerOrderNoList": [],
        "waybillNo": waybillNoList,
        "orderNo": "",
        "shipperName": "",
        "receiverName": "",
        "size": 20,
        "page": 1
    }
    response = requests.post(url, data=json.dumps(data), headers=headers)
    try:
        return response.json()
        # return response.json()["content"]["data"]
    except:
        logger.info('没有获取到运单id，请检查接口传参')
        return response.json()


def new_dispatch(shiftNo, waybillId):
    '''新建调度单接口,成功后会返回'''
    path = 'xx'
    url = http + ip + path
    # logger.debug('新建调度单接口完整网址{}'.format(url))
    headers = {'content-type': 'application/json;charset=UTF-8', 'token': token}
    # 调度单号 不能重复 shiftNo
    data = {
        "shiftNo": shiftNo,
        "projectName": "接口",
        "projectNo": "O1620357482152147",
        "carrierType": "YES",
        "shiftType": 2,
        "outsideDriverCarId": 3110,
        "demandCarInfoId": None,
        "ownCarId": None,
        "ownDriverId": None,
        "driverName": "李" + rand.randStr(5),
        "driverMobile": "15267045822",
        "licensePlate": "浙B" + str(rand.randInt(5)),
        "thirdContractName": "",
        "shiftWaybillDTOList": [{
            "waybillId": waybillId,  # 运单号id
            "receivableAmount": None
        }],
        "shiftFrom": "PC"
    }
    response = requests.post(url, data=json.dumps(data), headers=headers)
    '''返回调度单新建成功后返回的id'''
    try:
        # response.json()['content']['id']
        return response.json()
    except:
        logger.info('没有获取到调度单id，请检查接口传参')
        return response.json()


# print(new_dispatch('YS21051401090001111',223801))


def dispatch_waybill(shiftId):
    '''根据调度单新建成功后返回的id查看调度单里面的运单号是否为创建调度单的运单号'''
    path = 'xx'
    url = http + ip + path
    headers = {'content-type': 'application/json;charset=UTF-8', 'token': token}
    # 调度单id shiftId
    data = {'shiftId': shiftId}
    response = requests.post(url, data=json.dumps(data), headers=headers)
    '''返回调度单里面的订单号'''
    return response.json()['content']['waybillList']['data'][0]['waybillNo']


# print(dispatch_waybill(219564))

def send_orders(id):
    '''调度单派单--调度单id'''
    path = 'xx'
    url = http + ip + path
    headers = {'content-type': 'application/json;charset=UTF-8', 'token': token}
    # 调度单id列表 shiftIdList
    data = {"shiftIdList": [{"id": id}]}
    response = requests.post(url, data=json.dumps(data), headers=headers)
    return response.json()


# times=time_use()
# print(times.get_current_time(-1))
# print(type(times.get_current_time(-1)))

def depart(id):
    '''调度单发车--调度单id'''
    path = 'xx'
    url = http + ip + path
    headers = {'content-type': 'application/json;charset=UTF-8', 'token': token}
    data = {"shiftIdList": [{"id": id, "sendTime": time1.get_current_time(-1)}]}

    response = requests.post(url, data=json.dumps(data), headers=headers)
    return response.json()


def arrive(id):
    '''调度单到达---调度单id'''
    path = 'xx'
    url = http + ip + path
    headers = {'content-type': 'application/json;charset=UTF-8', 'token': token}
    data = {
        "shiftIdList": [{
            "id": id,
            "arriveTime": times.get_current_time()
        }]
    }
    response = requests.post(url, data=json.dumps(data), headers=headers)
    return response.json()


def receipt_affirm(id, waybillId):
    '''回单确认---运单id和运单号'''
    path = 'xx'
    url = http + ip + path
    headers = {'content-type': 'application/json;charset=UTF-8', 'token': token}
    data = {
        "waybillId": id,
        "waybillNo": waybillId,
        "receipt": "[\"https://xxoWK1HF6hhy/it/u=2827471950,3102544597&fm=26&gp=0.jpg\"]",
        "remark": ""
    }
    response = requests.post(url, data=json.dumps(data), headers=headers)
    return response.json()


def t1est(shiftId):
    '''根据调度单新建成功后返回的id查看调度单里面的运单号是否为创建调度单的运单号'''
    path = 'xx'
    url = http + ip + path
    headers = {'content-type': 'application/json;charset=UTF-8', 'token': token}
    # 调度单id shiftId
    data = {'shiftId': shiftId}
    response = requests.request('post', url, data=json.dumps(data), headers=headers)
    '''返回调度单里面的订单号'''
    return response.json()['content']['waybillList']['data'][0]['waybillNo']
